/**
 * @author banglong
 * @date 2016/8/18.
 */
require.config({
  shim: {
    jquery: ['avalon'],
    Layer: ['jquery'],
    layPage: ['jquery'],
  },
  paths: {
    avalon: '../../common/avalon@2.1.17',
    jquery: '../../common/jquery.min',
    text: '../../common/text',
    Layer: '../../common/layer/layer',
    rootPath: '../utils/rootPath',
    layPage: '../../common/laypage/laypage',
    tools: '../utils/tools',
    depsSelectDialog: '../components/bill/depsSelectDialog'
  }
})
require(['jquery', 'Layer', 'rootPath', 'depsSelectDialog'],
  function ($, layer, rootPath, depsSelectDialog) {

    layer.config({
      path: rootPath.getRootPath() + 'js/common/layer/'
    })

    // 日期添加 format 方法
    Date.prototype.format = function (format) {
      var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
      }
      if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
            date[k] : ("00" + date[k]).substr(("" + date[k]).length))
        }
      }
      return format
    }

    var vm = new Vue({
      el: '#app',
      data: {
        queryParam: {
          deptAccountID: '',
          deptAccountName: '',
          startTime: new Date(),
          endTime: new Date()
        },
        // 头表信息
        heads: [],
        // 明细表信息你
        details: [],
        // 科室信息
        depts: [],
      },
      methods: {
        // 选择科室
        chooseDept: function () {
          var self = this
          depsSelectDialog.init(self.depts, function (dept) {
            self.queryParam.deptAccountID = dept.DeptAccountID
            self.queryParam.deptAccountName = dept.Ksmc.trim()
          })
        },
        // 查询
        search: function () {
          if (this.queryParam.deptAccountID && this.queryParam.startTime && this.queryParam.endTime) {
            var self = this
            // 计算日期 结束时间需要加一
            var startTime = new Date(self.queryParam.startTime).format('yyyy-MM-dd')
            var n = new Date(self.queryParam.endTime).getTime() + 24 * 60 * 60 * 1000
            var endTime = new Date(n).format('yyyy-MM-dd')
            // 查询 账户余额信息
            $.ajax({
              type: 'POST',
              url: rootPath.getRootPath() + '/api/balanceQuery',
              xhrFields: {
                withCredentials: true
              },
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({
                startTime: startTime,
                endTime: endTime,
                deptAccountID: self.queryParam.deptAccountID,
                goodsBookID: '%'
              }),
              dataType: "json",
              success: function (data) {
                var result = JSON.parse(data.result)
                result.Details.forEach(function (item) {
                  switch (item.BillTypeStr) {
                    case 'lyzc':
                      item.BillTypeStr = '领用单'
                      break
                    case 'czzs':
                      item.BillTypeStr = '账户充值'
                      break
                  }
                })
                self.details = result.Details.concat()
                delete result.Details
                self.heads.splice(0, self.heads.length)
                self.heads.push(result)
              }
            })
          } else {
            alert('请确认查询参数是否完整')
          }
        }
      },
      mounted: function () {
        var self = this
        // 初始化日期
        self.queryParam.startTime = new Date().getFullYear() + '-01-01'

        // 加载科室信息
        $.ajax({
          type: 'POST',
          url: rootPath.getRootPath() + '/api/getDepartments',
          xhrFields: {
            withCredentials: true
          },
          contentType: "application/json; charset=utf-8",
          data: JSON.stringify({
            condition: 'null'
          }),
          dataType: "json",
          success: function (data) {
            self.depts = JSON.parse(data.result)
          }
        })
      }
    })

    // 监听 科室名称
    vm.$watch('queryParam.deptAccountName', function (newVal) {
      if (newVal === '') {
        vm.queryParam.deptAccountID = ''
      }
    })
  });